package com.six.core.util;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

/**
 * @author: WangPeng
 * @createTime: 2023/03/02 11:56
 * @description:
 * @version: v1.0
 */

public class UploadUtil {

    /**
     * 上传文件到阿里云oss
     * @param file 需要上传的文件
     * @return 返回文件路径
     */
    public static String ossUpload(MultipartFile file) {
        // Endpoint以华东1（杭州）为例，其它Region请按实际情况填写。
        String endpoint = "oss-cn-hangzhou.aliyuncs.com";
        // 阿里云账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维，请登录RAM控制台创建RAM用户。
        String accessKeyId = "LTAI5tS2nz7E3tFDzDqg5G1n";
        String accessKeySecret = "vB4MW2HX7M6oplKz4v3R1rLV8koRSB";
        // 填写Bucket名称，例如examplebucket。
        String bucketName = "w-peng";
        // 填写Object完整路径，完整路径中不能包含Bucket名称，例如exampledir/exampleobject.txt。
        String objectName = UUID.randomUUID() + file.getOriginalFilename();
        // 填写本地文件的完整路径，例如D:\\localpath\\examplefile.txt。
        // 如果未指定本地路径，则默认从示例程序所属项目对应本地路径中上传文件流。

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        try {
            InputStream inputStream = file.getInputStream();
            // 创建PutObject请求。
            ossClient.putObject(bucketName, objectName, inputStream);
            //https://w-peng.oss-cn-hangzhou.aliyuncs.com/1.jpg
            String path="https://"+bucketName+"."+endpoint+"/"+objectName;
            System.out.println("path = " + path);
            return path;
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        return null;
    }

    /**
     * 上传文件到本地
     * @param file 需要上穿的文件
     * @param request 服务器响应
     * @return 返回文件路径
     */
//    public static String upload(MultipartFile file, HttpServletRequest request) throws IOException {
//        //获取上传的路径
//        String path = request.getSession().getServletContext().getRealPath("upload");
//        File target = new File(path);
//        //判断文件夹target是否存在
//        if (!target.exists()) {
//            //如果文件夹不存在，创建文件夹
//            target.mkdirs();
//        }
//        //把上传的myfile文件转移到target文件下，使用IO流解决
//        //获取上传的文件名
//        String filename = file.getOriginalFilename();
//        File file1 = new File(path + "/" + filename);
//        //把上传的文件写到file中
//        file.transferTo(file1);
//        return path + "/" + filename;
//    }
}
